package tx.audioAndVideoRecorder;


import java.io.File;
import java.io.IOException;

import android.app.TabActivity;
import android.hardware.Camera;
import android.media.MediaRecorder;
import android.os.Environment;
import android.util.Log;
import android.view.SurfaceHolder;

public class MyVideoRecorder extends TabActivity {//implements SurfaceHolder.Callback{
	
	private static final String TAG = "MyVideoRecorder";
	private static final String VIDEO_RECORDER_FOLDER = "VideoRecorder";
	
	//File extensions (if we want to change video encoder)
	private static final String AUDIO_RECORDER_FILE_EXT_3GP = ".3gp";
    private static final String AUDIO_RECORDER_FILE_EXT_MP4 = ".mp4";
    private String file_exts[] = { AUDIO_RECORDER_FILE_EXT_MP4, AUDIO_RECORDER_FILE_EXT_3GP }; 
    private String mlastFileCreated = null;
	
		
    private SurfaceHolder mSurfaceHolder;
	private Camera mCamera;
    
	// Recorder information
	private MediaRecorder mediaRecorder;
	private final int maxDurationInMs = 20000;
	private final long maxFileSizeInBytes = 500000;
	private final int videoFramesPerSecond = 20;
	
	
	public MyVideoRecorder(SurfaceHolder aSurface, Camera aCamera){
		mSurfaceHolder = aSurface;
		mCamera = aCamera;
	
	}	
	
	// Start recording
	public boolean startRecording(){
		try {
			mCamera.unlock();

			mediaRecorder = new MediaRecorder();

			mediaRecorder.setCamera(mCamera);
			mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
			mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

			mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);

			mediaRecorder.setMaxDuration(maxDurationInMs);

			mediaRecorder.setOutputFile(getFilename());

			mediaRecorder.setVideoFrameRate(videoFramesPerSecond);
			//mediaRecorder.setVideoSize(surfaceView.getWidth(), surfaceView.getHeight());
			mediaRecorder.setVideoSize(240, 160);

			mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
			mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);

			mediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());

			mediaRecorder.setMaxFileSize(maxFileSizeInBytes);

                        mediaRecorder.prepare();
			mediaRecorder.start();

			return true;
		} catch (IllegalStateException e) {
			Log.e(TAG,e.getMessage());
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			Log.e(TAG,e.getMessage());
			e.printStackTrace();
			return false;
		}
	}
	
	//Stop recording 
	public void stopRecording(){
		try {
			mediaRecorder.stop();
		} catch (Exception e) {
			Log.e(TAG,e.getMessage());
		}
		
		mCamera.lock();
	}
	
	public String getFilename(){
        String filepath = Environment.getExternalStorageDirectory().getPath();
        File file = new File(filepath,VIDEO_RECORDER_FOLDER);
        
        if(!file.exists()){
                file.mkdirs();
        }
        // TODO : Replay video
        mlastFileCreated = file.getAbsolutePath() + "/" + System.currentTimeMillis() + AUDIO_RECORDER_FILE_EXT_MP4;
        return (mlastFileCreated);
	}

	private void setLastFileCreated(String mlastFileCreated) {
		this.mlastFileCreated = mlastFileCreated;
	}

	private String getLastFileCreated() {
		return mlastFileCreated;
	}
	
	
}
